const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    info: {},
    focus: false,
    reply_text: '填写评论……',
    reply_nickname: '',
    navigator: { hide: 0 },
    reply_openid: '',
    pian:0
  },
  follow: function(e){
    var info = this.data.info;
    var that = this;
    wx.showLoading({
      title: '加载中…',
    })
    var data = {};
    data.openid = app.globalData.openid;
    data.follow_openid = info.follow_openid;
    app.ihttpPost('https://www.58zltc.com/api.php/Quan/follow', data, function(res){
      if (res.data.err_code === 0) {
        if (res.data.err_msg == '-1') {
          info.is_follow = false;
        } else {
          info.is_follow = true;
        }
        that.setData({
          info: info
        })
        wx.hideLoading()
      } else {
        wx.showToast({
          title: res.data.err_msg
        })
      }
    })
  },
  comment: function (e) {
    var reply_nickname = e.currentTarget.dataset.nickname;
    var reply_openid = e.currentTarget.dataset.openid;
    var reply_text = "回复" + reply_nickname + ":";
    var openid = app.globalData.openid;
    if (reply_openid != openid) {
      console.log(reply_openid);
      this.setData({
        focus: true,
        reply_text: reply_text,
        reply_openid: reply_openid,
        reply_nickname: reply_nickname,
        bottom: 50,
      })
    }

  },
  //快捷导航
  navigator: function (e) {
    var width = this.data.navigator.width
    if (this.data.navigator.hide == 1) {
      var offset = '0'
      var mum = 0;
    } else {
      var offset = width * 0.64
      var mum = 1;
    }
    this.animation.translateX(offset).step()
    var v = this.data.navigator
    v.animation = this.animation.export();
    v.hide = mum;
    this.setData({
      //输出动画
      navigator: v
    })
  },

  setReplyText: function (e) {
    this.setData({
      reply_text: '填写评论……',
      reply_openid: '',
      bottom: 0,
    })
  },
  zan: function (e) {
    let extConfig = wx.getExtConfigSync ? wx.getExtConfigSync() : {};
    var info = this.data.info;
    var that = this;
    wx.request({
      url: 'https://www.58zltc.com/api.php/Quan/zan?openid=' + app.globalData.openid + '&id=' + info.id + '&token=' + extConfig.appid,
      success: res => {
        if (res.data.err_code === 0) {
          if (res.data.err_msg == '-1') {
            info.zan_num = parseInt(info.zan_num) - 1;
            info.is_zan = false;
          } else {
            info.zan_num = parseInt(info.zan_num) + 1;
            info.is_zan = true;
          }
          that.setData({
            info: info
          })
          
        } else {
          wx.showToast({
            title: res.data.err_msg
          })
        }
      }
    })
  },
  reply: function(e){
    let that = this;
    var value = e.detail.value;
    if(!value){
      return false;
    }
    var post_data = [];
    post_data.content = value;
    post_data.openid = app.globalData.openid;
    post_data.id = that.data.info.id;
    post_data.reply_openid = that.data.reply_openid;
    post_data.reply_nickname = that.data.reply_nickname;
    post_data.reply_part = 'quan';
    app.ihttpPost("https://www.58zltc.com/api.php/Quan/reply",post_data, function(res){
      if (res.data.err_code === 400) {
        wx.showToast({
          title: res.data.err_msg,
        })
      } else if (res.data.err_code === 0) {
        wx.showToast({
          title: res.data.err_msg,
          icon: 'success'
        })
        setTimeout(function () {
          that.reShow();
        }, 1500)
      }
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    wx.showLoading({
      title: '加载中…',
    })
    this.setData({
      id: options.id
    })
    var that =this
    //骗审
    app.ihttpGet("https://www.58zltc.com/api.php/Index/pian", function (res) {
      if (res.data.pian == 1) {
        that.setData({
          pian: 1
        })
      }
    })
    //快速导航
    app.ihttpGet("https://www.58zltc.com/api.php/Index/getNav", function (res) {
      var v = that.data.navigator;
      v.info = res.data
      that.setData({
        navigator: v
      })
    })
    //屏幕宽度
    wx.getSystemInfo({
      success: function (res) {
        var width = res.screenWidth;
        var v = that.data.navigator;
        v.width = width
        that.setData({
          navigator: v
        })
      },
    })

    //实例化一个动画
    this.animation = wx.createAnimation({
      // 动画持续时间，单位ms，默认值 400
      duration: 300,
      /**
       * linear 动画一直较为均匀
       * ease  从匀速到加速在到匀速
       * ease-in 缓慢到匀速
       * ease-in-out 从缓慢到匀速再到缓慢
       * step-start 动画一开始就跳到 100% 直到动画持续时间结束 一闪而过
       * step-end  保持 0% 的样式直到动画持续时间结束    一闪而过
       */
      timingFunction: 'ease',
      // 延迟多长时间开始
      delay: 100,
      /**
       * 以什么为基点做动画 效果自己演示
       * left,center right是水平方向取值，对应的百分值为left=0%;center=50%;right=100%
       * top center bottom是垂直方向的取值，其中top=0%;center=50%;bottom=100%
       */
      transformOrigin: 'right',
      success: function (res) {

      }
    })
    var width = this.data.navigator.width;
    var offset = width * 0.64;
    that.animation.translateX(offset).step()
    var v = this.data.navigator
    v.animation = that.animation.export()
    v.hide = 1
    that.setData({
      //输出动画
      navigator: v
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
  
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.reShow();
  },
  reShow: function(){
    const id = this.data.id;
    let extConfig = wx.getExtConfigSync ? wx.getExtConfigSync() : {};
    wx.request({
      url: 'https://www.58zltc.com/api.php/Quan/item?openid=' + app.globalData.openid + '&id=' + id + '&token=' + extConfig.appid,
      success: res => {
        this.setData({
          info: res.data
        })
        
      }
    })
    wx.hideLoading();
  },
  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
  
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
  
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
  
  },


  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
  
  },
  backto: function () {
    wx.navigateBack({})
  }
})